package com.cuit.riskassessment.action.depart;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuit.riskassessment.service.DepartInfoService;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.vo.DepartInfo;

@Controller
@RequestMapping(value = "departInfoAction")
public class DepartInfoAction {
	@Resource(name = "departInfoServiceObject")
	private DepartInfoService service;

	@RequestMapping(value = "get")
	public @ResponseBody
	List<DepartInfo> get(@RequestParam("name") String name,
			@RequestParam("val") String val) throws UnsupportedEncodingException {
		name =new String(name.getBytes("iso8859_1"),"utf-8");
		if (name == null) {
			return service.getAll();
		}
		if (val == null || val.equals("")) {
			return service.getByElement(name, "VC_NAME");
		}
		return service.getByElement(name, val);
	}
	@RequestMapping(value = "addOrUpdate")
	public @ResponseBody Object addOrUpdate(HttpServletResponse response ,DepartInfo departInfo){
		response.setHeader("Accept", "text/html");
		Result res = new Result();
		if (departInfo.getId()==null||departInfo.getId().equals("")) {
			if(service.getCd(departInfo.getCd())){
				return 1/0;
			}else {
				service.add(departInfo);
			}
		}else {
			service.update(departInfo);
		}
		return res;
	}
	@RequestMapping(value = "del")
	public @ResponseBody Object del(String id){
		Result res = new Result();
		res.setSuccess(true);
		service.del(id);
		return res;
	}
}

